<?php
/**
 * @特惠赚、广告控制器
 * Author: 小谢     
 * Date: 2019/05/20
 */
namespace app\admin\controller;
use think\facade\Session;
use think\facade\View;
use think\Db;

class Ad extends Base {
    
    /**
     *  广告列表
     */
    public function adList() {
        $p = I('p/d',1);
        $list = M('ad')->alias('a')->join('__AD_POSITION__ p','a.pid = p.position_id')->where(['enabled'=>1])->order('orderby ASC')->page($p.',20')->paginate(20);
        $count = $list->count();
        View::share(['list'=>$list,'count'=>$count]);
        return View();
    }


    /**
     * 广告信息
     */
    public function ad_info() {
        $id = I('get.ad_id/d');
        $info = M('ad')->where('ad_id',$id)->find();
        $position = M('ad_position')->select();
        View::share(['info'=>$info,'position'=>$position]);
        return View('_ad');
    }

    /**
     * [adHandle 广告信息操作]
     */
    public function adHandle() {
        $data = I('post.');
        if($data['ad_id'] > 0) {
            $res = M('ad')->where(['ad_id'=>$data['ad_id']])->update($data);
        } else {
            if ($data) {
                $res = M('ad')->add($data);
            }
        }

        if(isset($res)) {
            $this->success('操作成功',U('/Admin/ad/adList'));
        }
        $this->error('操作失败',U('/Admin/ad/adList'));
    }

    /**
     * [del 删除操作]
     */
    public function del(){
        $data = I('post.');
        if($data['id'] > 0 ){
            $res = M('ad')->where('ad_id', $data['id'])->delete();
            if($res){
                $this->ajaxReturn(['status'=>1,'msg'=>"操作成功",'url'=>U('Admin/Ad/adList')]);
            }else{
                $this->ajaxReturn(['status'=>-1,'msg'=>"操作失败"]);
            }
        }
    }

    /**
     * 广告位列表
     */
    public function positionList() {
        $p = I('p/d',1);
        $list = M('ad_position')->order('position_id ASC')->page($p.',20')->paginate(20);
        $count = $list->count();
        View::share(['list'=>$list,'count'=>$count]);
        return View();
    }

   /**
     * 广告位信息
     */
    public function position_info() {
        $id = I('get.position_id/d');
        $info = M('ad_position')->where('position_id',$id)->find();
        View::share(['info'=>$info]);
        return View('_position');
    }

    /**
     * [adHandle 广告位信息操作]
     */
    public function positionHandle() {
        $data = I('post.');
        if($data['position_id'] > 0) {
            $res = M('ad_position')->where(['position_id'=>$data['position_id']])->update($data);
        } else {
            if ($data) {
                $res = M('ad_position')->add($data);
            }
        }
        if(isset($res)) {
            $this->success('操作成功',U('/Admin/ad/positionList'));
        }
        $this->error('操作失败',U('/Admin/ad/positionList'));
    }

    /**
     * [del 删除操作]
     */
    public function positionDel(){
        $data = I('post.');
        if($data['id'] > 0 ){
            $res = M('ad_position')->where('position_id', $data['id'])->delete();
            if($res){
                $this->ajaxReturn(['status'=>1,'msg'=>"操作成功",'url'=>U('Admin/Ad/positionList')]);
            }else{
                $this->ajaxReturn(['status'=>-1,'msg'=>"操作失败"]);
            }
        }
    }


}